class Solution {
 public:
  TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    TreeNode* root = CreateTree(nums, 0, nums.size() - 1);
    return root;
  }
  TreeNode* CreateTree(vector<int>& nums, int l, int r) {
    if (l > r) return nullptr;
    int index = l;
    int Max = INT_MIN;
    for (int i = l; i <= r; i++) {
      if (nums[i] > Max) {
        Max = nums[i];
        index = i;
      }
    }
    TreeNode* node = new TreeNode(nums[index]);
    node->left = CreateTree(nums, l, index - 1);
    node->right = CreateTree(nums, index + 1, r);
    return node;
  }
};
// 与通过遍历序列建树类似，还更简单